#
#	Specify :
#		DEBUG=y				# for a debug build
#		PSPGPROF=y			# for profiling with psp gprof
#
#		CONFIG=<configname>		# Build using Source/Configs/<configname> as config
#
#		Default config for DEBUG build is Dev
#		Default config for non DEBUG build is Release
#

TARGET = Daedalus

DAED_MAIN_SRCS =	Source/ConfigOptions.cpp \
			Source/Test/BatchTest.cpp

DAED_DEBUG_SRCS =	Source/Debug/DebugLog.cpp \
			Source/Debug/Dump.cpp

DAED_CORE_SRCS =	Source/System.cpp \
			Source/Core/CPU.cpp \
			Source/Core/DMA.cpp \
			Source/Core/Interrupts.cpp \
			Source/Core/Memory.cpp \
			Source/Core/PIF.cpp \
			Source/Core/R4300.cpp \
			Source/Core/Registers.cpp \
			Source/Core/ROM.cpp \
			Source/Core/RomSettings.cpp \
			Source/Core/ROMBuffer.cpp \
			Source/Core/ROMImage.cpp \
			Source/Core/RSP.cpp \
			Source/Core/RSP_HLE.cpp \
			Source/Core/Savestate.cpp \
			Source/Core/TLB.cpp \
			Source/Core/Dynamo.cpp \
			Source/Core/Interpret.cpp \
			Source/Core/Save.cpp \
			Source/Core/JpegTask.cpp \
			Source/Core/FlashMem.cpp \
			Source/Core/Cheats.cpp

DAED_INTERFACE_SRCS =	Source/Interface/RomDB.cpp

DAED_INPUT_SRCS =	
##Source/SysSDL/Input/InputManagerPSP.cpp

DAED_DYNREC_SRCS = ## 	Source/SysSDL/DynaRec/AssemblyUtilsPSP.cpp \
			##Source/SysSDL/DynaRec/AssemblyWriterPSP.cpp \
			##Source/SysSDL/DynaRec/CodeBufferManagerPSP.cpp \
			##Source/SysSDL/DynaRec/CodeGeneratorPSP.cpp \
		##	Source/SysSDL/DynaRec/DynarecTargetPSP.cpp \
		##	Source/SysSDL/DynaRec/N64RegisterCachePSP.cpp \
		##	Source/SysSDL/DynaRec/DynaRecStubs.S \
			Source/DynaRec/BranchType.cpp \
			Source/DynaRec/DynaRecProfile.cpp \
			Source/DynaRec/Fragment.cpp \
			Source/DynaRec/FragmentCache.cpp \
			Source/DynaRec/IndirectExitMap.cpp \
			Source/DynaRec/StaticAnalysis.cpp \
			Source/DynaRec/TraceRecorder.cpp

DAED_UTILITY_SRCS =	Source/Utility/CRC.cpp \
			Source/Utility/FramerateLimiter.cpp \
			Source/Utility/IniFile.cpp \
			Source/Utility/Hash.cpp \
			Source/Utility/MemoryHeap.cpp \
			Source/Utility/Preferences.cpp \
			Source/Utility/Profiler.cpp \
			Source/Utility/ROMFile.cpp \
			Source/Utility/ROMFileMemory.cpp \
			Source/Utility/ROMFileCache.cpp \
			Source/Utility/ROMFileCompressed.cpp \
			Source/Utility/ROMFileUncompressed.cpp \
			Source/Utility/Stream.cpp \
			Source/Utility/Timer.cpp \
			Source/Utility/Translate.cpp \
			Source/Utility/ZLibWrapper.cpp \
			Source/Utility/PrintOpCode.cpp \
			Source/Math/Matrix4x4.cpp

DAED_HLEGFX_SRCS =	Source/Plugins/GraphicsPlugin.cpp \
			Source/HLEGraphics/Blender.cpp \
			Source/HLEGraphics/BlendModes.cpp \
			Source/HLEGraphics/ColourAdjuster.cpp \
			Source/HLEGraphics/ConvertImage.cpp \
			Source/HLEGraphics/DisplayListDebugger.cpp \
			Source/HLEGraphics/DLParser.cpp \
			Source/HLEGraphics/Microcode.cpp \
			Source/HLEGraphics/RDP.cpp \
			Source/HLEGraphics/RDPStateManager.cpp \
			Source/HLEGraphics/PSPRenderer.cpp \
			Source/HLEGraphics/Texture.cpp \
			Source/HLEGraphics/TextureCache.cpp \
			Source/HLEGraphics/TextureDescriptor.cpp \
			Source/HLEGraphics/uCodes/Ucode.cpp \
			Source/HLEGraphics/Combiner/CombinerExpression.cpp \
			Source/HLEGraphics/Combiner/CombinerTree.cpp \
			Source/HLEGraphics/Combiner/RenderSettings.cpp

DAED_AUDIO_SRCS =  	Source/HLEAudio/ABI1.cpp \
			Source/HLEAudio/ABI2.cpp \
			Source/HLEAudio/ABI3.cpp \
			Source/HLEAudio/ABI3mp3.cpp \
			Source/HLEAudio/AudioBuffer.cpp \
			Source/HLEAudio/AudioHLEProcessor.cpp \
			Source/HLEAudio/HLEMain.cpp \

DAED_OSHLE_SRCS = 	Source/OSHLE/OS.cpp \
			Source/OSHLE/patch.cpp


ADDITIONAL_SYNC_SRCS  = Source/Utility/Synchroniser.cpp Source/Utility/ZLibWrapper.cpp

CORE_SRCS = $(DAED_MAIN_SRCS) $(DAED_DEBUG_SRCS) $(DAED_CORE_SRCS) $(DAED_INTERFACE_SRCS) \
	    $(DAED_INPUT_SRCS) $(DAED_DYNREC_SRCS) $(DAED_UTILITY_SRCS) $(DAED_PSP_SRCS) \
	    $(DAED_HLEGFX_SRCS) $(DAED_AUDIO_SRCS) $(DAED_OSHLE_SRCS)
		
ifdef SDLGPROF	
	CONFIG=Profile
	
	CFLAGS			= -pg -g -O2 -G0 -D_DEBUG -Wall -MD -ffast-math -fsingle-precision-constant

	SRCS			= $(CORE_SRCS) $(DAED_GPROF_SRCS) 
else 
ifdef DEBUG
	CONFIG=Dev #default config in Debug build is "Dev"

	CFLAGS			= -g -O1 -fno-omit-frame-pointer -G0 -D_DEBUG -MD \
				  -W -Wcast-qual -Wchar-subscripts -Wno-unused -Wpointer-arith\
				  -Wredundant-decls -Wshadow -Wwrite-strings
				#-Winline -Wcast-align 

	SRCS			= $(CORE_SRCS) $(ADDITIONAL_SYNC_SRCS)
else 
	CFLAGS			= -O2 -G0 -DNDEBUG -Wall -MD -ffast-math -fsingle-precision-constant -fpredictive-commoning -mno-check-zero-division
					#-pipe
					#-Wextra
					#-fno-builtin
					#-fgcse-after-reload
					#-funroll-loops 
	LDFLAGS 		= "-Wl,-O1"

	SRCS			= $(CORE_SRCS)
endif
endif

ifndef CONFIG
	CONFIG=Release
endif

CXXFLAGS = -iquote./Source/SysSDL/Include -iquote./Source/Config/$(CONFIG) -iquote./Source -iquote./Source/SysSDL

OBJS := $(SRCS:.cpp=.o)
OBJS := $(OBJS:.c=.o)
OBJS := $(OBJS:.S=.o)

DEP_FILES := $(SRCS:.cpp=.d)
DEP_FILES := $(DEP_FILES:.c=.d)
DEP_FILES := $(DEP_FILES:.S=.d)

ASFLAGS =


LIBS = -lstdc++ -lc -lpng -lz -lg -lm -lGLES_CM

EXTRA_CLEAN=$(DEP_FILES)


DATA_DIR = ../data
BUILDS_DIR = ./Builds
BUILDS_PSP_DIR = $(BUILDS_DIR)/PSP
BUILDS_GAME_DIR = $(BUILDS_PSP_DIR)/GAME
BUILDS_DX_DIR = $(BUILDS_GAME_DIR)/DaedalusX64


#RESULT := $(shell LC_ALL=C svn info | grep Revision | grep -e [0-9]* -o | tr -d '\n') #does not work with Windows...
VERSION = $(shell svnversion -n 2> Makefile.cache)


-include $(DEP_FILES)

$(TARGET): $(OBJS)
	$(LINK.c) $^ $(LIBS) -o $@
	$(FIXUP) $@

$(TARGET_LIB): $(OBJS)
	$(AR) cru $@ $(OBJS)
	$(RANLIB) $@

%.o: %.m
	$(CC) $(CFLAGS) -c -o $@ $<

%.o: %.mm
	$(CXX) $(CXXFLAGS) -c -o $@ $<

clean: 
	-$(RM) -f $(FINAL_TARGET) $(EXTRA_CLEAN) $(OBJS) $(EXTRA_TARGETS) 

rebuild: clean all


